<?php

namespace app\api\controller;

use app\api\Validate\EducationValidate;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 教育经历
 */
class Education extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $failException = true;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\Education();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     * 列表
     * @return void
     */
    public function list()
    {
        $input = input();
        try {
            $where['user_id'] =$this->auth->getUserinfo()['id'];
            if (!empty($input['id'])){
                $where['id'] = $input['id'];
            }
            $data = $this->model->where($where)
                ->order('id desc')
                ->select();
            $this->success('ok', $data);
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 添加
     * @return void
     */
    public function add()
    {
        $input = input('post.');
        try {
            $input['user_id'] =  $this->auth->getUserinfo()['id'];
            $this->validate($input, EducationValidate::class . '.add');
            $this->model->insert($input);
            $this->success('ok');
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     * 保存
     * @return void
     */
    public function save()
    {
        $input = input('post.');
        try {
            $this->validate($input, EducationValidate::class . '.save');
            $id = $input['id'];
            unset($input['id']);
            $this->model->where('id', $id)->update($input);
            $this->success('ok', ['id' => $id]);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
}